﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ContractManagermentSystem.Common;
using ContractManagermentSystem.Entity;

namespace ContractManagermentSystem.DataAccess
{
    public class RoleDao
    {
        private readonly ConnectionHelper _connectionHelper;

        public RoleDao()
        {
            _connectionHelper = new ConnectionHelper();
        }

        public void CreateRole(Role role)
        {
            var parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@RoleName", SqlDbType.NVarChar, 30) { Value = role.RoleName };

            parameters[1] = new SqlParameter("@Status", SqlDbType.Bit) { Value = role.Status };

            _connectionHelper.ExecuteNonQuery("sp_CreateRole", parameters);
        }

        public int CheckRoleName(string roleName)
        {
            var parameter = new SqlParameter("@RoleName", SqlDbType.NVarChar, 30) { Value = roleName };

            return _connectionHelper.ExecuteScalar("sp_CheckRoleNameExisted", parameter);
        }

        public DataTable LoadAllRole()
        {
            return _connectionHelper.ExecuteDataTable("sp_GetAllRole", null);
        }

        public void ChangeStatusRole(Role role)
        {
            var parameters = new SqlParameter[2];
            parameters[0] = new SqlParameter("@RoleId", SqlDbType.UniqueIdentifier) { Value = role.RoleId };

            parameters[1] = new SqlParameter("@Status", SqlDbType.Bit) { Value = role.Status };

            _connectionHelper.ExecuteNonQuery("sp_ChangeStatusRole", parameters);
        }

        public DataTable GetRoleById(Guid roleId)
        {
            var parameter = new SqlParameter("@RoleId", SqlDbType.UniqueIdentifier) { Value = roleId };

            return _connectionHelper.ExecuteDataTable("sp_GetRoleById", parameter);
        }

        public void DeleteRoleById(Role role)
        {
            var parameter = new SqlParameter("@RoleId", SqlDbType.UniqueIdentifier) { Value = role.RoleId };
            _connectionHelper.ExecuteNonQuery("sp_DeleteRoleById", parameter);
        }

        public void UpdateRoleById(Role role)
        {
            var parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@RoleId", SqlDbType.UniqueIdentifier) { Value = role.RoleId };
            parameters[1] = new SqlParameter("@RoleName", SqlDbType.NVarChar, 30) { Value = role.RoleName };
            parameters[2] = new SqlParameter("@Status", SqlDbType.Bit) { Value = role.Status };
            _connectionHelper.ExecuteNonQuery("sp_UpdateRoleById", parameters);
        }
    }
}
